package 题目集.动态规划.背包问题;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * 第三题：https://www.nowcoder.com/exam/test/83925253/detail?pid=55292243&examPageSource=Company&testCallback=https%3A%2F%2Fwww.nowcoder.com%2Fexam%2Fcompany%3FcurrentTab%3Drecommand%26jobId%3D100%26keyword%3D%E8%9A%82%E8%9A%81%26selectStatus%3D0&testclass=%E8%BD%AF%E4%BB%B6%E5%BC%80%E5%8F%91
 * 分组背包问题,记忆化搜索版
 * 设结果为：dp[i][j]:预算为j，选择i件物品后的最大性价比
 * dp[i][j]=max{dp[i-1][j-p[0]]+v[0],dp[i-1][j-p[1]]+v[1]...}
 */
public class ch06_2_必须选择的分组背包 {
    static int maxn = 50;
    static int maxv = (int) 1e9 + 10;
    static int[][][] w = new int[maxn][maxn][2];
    static int n, x;
    static int[] lens = new int[maxn];
    //    static int[][] dp = new int[maxn][maxv];  /*堆溢出*/
    static Map<String, Long> dp = new HashMap<>();

    public static void main(String[] args) {
        Reader2 sc = new Reader2();
        n = sc.nextInt();
        x = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            lens[i] = sc.nextInt();
            for (int j = 1; j <= lens[i]; j++) {
                w[i][j][0] = sc.nextInt();
            }
            for (int j = 1; j <= lens[i]; j++) {
                w[i][j][1] = sc.nextInt();
            }
        }
        long res = dfs(n, x);
        System.out.println(res == Long.MIN_VALUE ? -1 : res);
    }

    /**
     * 总价为j，有i件选择
     */
    public static long dfs(int i, int j) {
        if (i == 0) return 0;
        String key = i + ":" + j;
        if (dp.get(key) != null) {
            return dp.get(key);
        }
        long res = Long.MIN_VALUE;
        /*i零件的每种型号*/
        for (int k = 1; k <= lens[i]; k++) {
            if (j >= w[i][k][0]) {
                long dfs = dfs(i - 1, j - w[i][k][0]);
                if (dfs != Long.MIN_VALUE) {
                    res = Math.max(res, dfs+w[i][k][1]);
                }
            }
        }
        dp.put(key, res);
        return res;
    }

    static class Reader2 {
        static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));

        public Reader2(Object... o) {
        }

        public int nextInt() {
            try {
                in.nextToken();
            } catch (Exception ignored) {
            }
            return (int) in.nval;
        }
    }

}
